/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deadlocksim.gui;

/**
 *
 * @author Jabobo
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
/**
 *
 * @author m306587
 */
public class GradientButton extends JButton
{
    private Color baseColor1;
    private Color baseColor2;
    private Color hoverColor;
    
    private Color color1;
    private Color color2;
    private boolean isFlipped;

    public GradientButton() 
    {
        this(null);
    }
    
    public GradientButton(String text)
    {
        this(text, GuiConfig.BUT_COLOR_1, GuiConfig.BUT_COLOR_2, GuiConfig.BUT_COLOR_3);
    }

    public GradientButton(Color c1, Color c2, Color hoverColor) 
    {
        this(null, c1, c2, hoverColor);
    }
    public GradientButton(String text, Color c1, Color c2, Color hoverColor) 
    {
        super();
        if(c1 == null)
            c1 = super.getBackground();
        if(c2 == null)
            c2 = super.getBackground();
        this.baseColor1 = c1;
        this.baseColor2 = c2;
        this.hoverColor = hoverColor;
        this.color1 = baseColor1;
        this.color2 = baseColor2;
        isFlipped = false;
        setContentAreaFilled(false);
        if(text != null)
        {
            setText(text);
        }
        //setFont(GuiConfig.BUT_FONT);
        //setBorder(GuiConfig.BUT_RAISED_BORDER);
        setupMouseActions();
       
    }
    public void setColors(Color c1, Color c2, Color hover)
    {
        setBaseColor1(c1);
        setActiveColor1(c1);
        setBaseColor2(c2);
        setActiveColor2(c2);
        setHoverColor(hover);
    }
    public void setBaseColor1(Color c1)
    {
        this.baseColor1 = c1;
    }
    public void setBaseColor2(Color c2)
    {
        this.baseColor2 = c2;
    }
    public void setHoverColor(Color c3)
    {
        hoverColor = c3;
    }

    public void setActiveColor1(Color c1) 
    {
        this.color1 = c1;
        repaint();
    }

    public void setActiveColor2(Color c2) 
    {
        this.color2 = c2;
        repaint();
    }
    // Pass only the NAME of a file, not the path
    
    public boolean setIcon(String imageName)
    {
        imageName = imageName.toLowerCase();
        String imagePath =  "images/"+ imageName;
        File imageFile = new File(imagePath);
        return setIcon(imageFile);
       
    }
    public boolean setIcon(File imageFile)
    {
        String imagePath = "";
        if(imageFile != null && imageFile.isFile())
        {
            imagePath = imageFile.getAbsolutePath();
            setIcon(new ImageIcon(imagePath));
            return true;
        }
        else
        {
            System.err.println("\nFailed to load image; " + imagePath + "is not a file.");
            return false;
        }
    }
    public void setupMouseActions()
    {
         
        this.addMouseListener(new MouseAdapter()
                {

                    //when in scope , highlight
                    public void mouseEntered(MouseEvent me)
                    {
                        //setBorder(passingBorder);
                        if( isFlipped())
                        {
                            setActiveColor2(hoverColor);
                        }
                        else
                        {
                            setActiveColor1(hoverColor);
                        }
                        //setForeground(textHighlight);
                    }
                    //when out of scope restore raised
                    public void mouseExited(MouseEvent me)
                    {
                        if( isFlipped())
                        {
                            setActiveColor2(baseColor1);
                        }
                        else
                        {
                            setActiveColor1(baseColor1);
                        }
                        //setBorder(GuiConfig.BUT_RAISED_BORDER);
                        //setForeground(GuiConfig.BUT_TEXT_COLOR);
                    }
                    //when out of scope restore raised
                    public void mousePressed(MouseEvent me)
                    {
                        setBorder(GuiConfig.BUT_LOWERED_BORDER);
                        setForeground(GuiConfig.BUT_TEXT_COLOR.darker());
                        flipColors();
                    }
                    //when out of scope restore raised
                    public void mouseReleased(MouseEvent me)
                    {
                        setBorder(GuiConfig.BUT_RAISED_BORDER);
                        setForeground(GuiConfig.BUT_TEXT_COLOR);
                        flipColors();
                    }
                        
                        
		});
    }
    public void flipColors()
    {
        Color temp = color1;
        color1 = color2;
        color2 = temp;
        isFlipped = !isFlipped;
        repaint();
    }
    public boolean isFlipped()
    {
        return isFlipped;
    }

    // Overloaded in order to paint the background
    protected void paintComponent(Graphics g) 
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth();
        int h = getHeight();
        GradientPaint gradient 
                = new GradientPaint(20, 0, color1, 20, h, color2, false);
        g2.setPaint(gradient);
        g2.fillRect(0, 0, w, h);
        super.paintComponent(g);
    }
}

